/*
 * Client class for echo project
 * Author: Daniel Henderson
 * TCSS 430 Summer 2013 
 * University of Washington
 */
package ClientMulti;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

public class mClient {

	/**
	 * Ip address or host name of Multicast group
	 */
	private static String host = null;
	
	/**
	 * Port for the Multicast client to connect to group
	 */
	private static int portNumber = 0;
	
	/**
	 * InetAdress Object to hold the host name Ip address
	 */
	private static InetAddress IP;
	
	/**
	 * Socket to be used for Multiclass socket
	 */
	private static MulticastSocket mSocket;

	/**
	 * @param args args[0] IP address of multicast group
	 * @param args args[1] Port of multicast group
	 */
	public static void main(String[] args) {
		try
		{
			host = args[0];//arguments args[];
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println("Please try again missing starting parameters");
			System.exit(0);
		}
		try
		{
			portNumber = Integer.parseInt(args[1]);
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println("Please try again missing starting parameters");
			System.exit(0);
		}
		
		System.out.println("Creating socket to '" + host + "' on port " + portNumber);
		createMultiSocket();
		createIPObject();
		joinGroup();
			
		class getMessages implements Runnable {
			@Override
			public void run() {
				while(true) {
					 byte[] buf = new byte[1000];
					 DatagramPacket recv = new DatagramPacket(buf, buf.length);
					 try {
						mSocket.receive(recv);
						String received = new String(recv.getData());
						System.out.println("Message from server: " + received.trim());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}				
			}			
		}	
		getMessages theMessages = new getMessages();
		new Thread(theMessages).start();
	}
	
	/**
	 * Creates the Multicast socket for the client 
	 */
	private static void createMultiSocket() {
		try {
			mSocket = new MulticastSocket(portNumber);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	/**
	 * Takes the host name (IP) and creates an
	 * IPAdress Object of that host.
	 */
	private static void createIPObject() {
		try {
			IP = InetAddress.getByName(host);
		} catch (UnknownHostException e) {
			System.out.println("The address provided was invalid, or server is offline. Please try again");
			e.printStackTrace();
	
		}
	}
	
	/**
	 * Attempts to join the group with the specified IP Object
	 * and port for the multicast group.
	 */
	private static void joinGroup() {
		try {
			mSocket.joinGroup(IP);
		} catch (IOException e) {
			System.out.println("Unable to join the multicast group at specified IP address. Please try again");
			e.printStackTrace();
		}
	}
	
	/**
	 * Attempts to leave the multicast group.
	 */
	private static void leaveGroup() {
		try {
			mSocket.leaveGroup(IP);
		} catch (IOException e) {
			System.out.println(" Error: Unable to leave multicast group");
			e.printStackTrace();
		}
		mSocket.close();
	}

}
